#!/bin/bash
#script based on remastersys

GRUBFILE="$1"
if [ -f $GRUBFILE ]; then
rm -f $GRUBFILE
fi

ROOTMOUNT=`echo $GRUBFILE | sed -e 's/boot\/grub\/menu.lst//' | sed -e 's/\/$//'`
if [ "$ROOTMOUNT" = "" ]; then
ROOTMOUNT="/"
fi

ROOTPART=`mount | grep " $ROOTMOUNT " | awk '{print $1}'`
GRUBLOCTEST="$2"
if [ "$GRUBLOCTEST" = "root" ]; then
GRUBLOC="$ROOTPART"
else
GRUBLOC="hd0"
fi



if [ "$ROOTMOUNT" != "/" ]; then
chroot $ROOTMOUNT grub-install --no-floppy $GRUBLOC
chroot $ROOTMOUNT cp /opt/BOSSinstaller/splash.xpm.gz /boot/grub/splash.xpm.gz
chroot $ROOTMOUNT update-grub
else
grub-install --no-floppy $GRUBLOC
cp /opt/BOSSinstaller/splash.xpm.gz /boot/grub/splash.xpm.gz
update-grub
fi

#ugly fix as update-grub not run on the actual partition will not use the proper root=UUID= entry so get it from fstab and replace it if it finds it
#will do nothing if it is normal
ROOTENTRY=`grep " / " $ROOTMOUNT/etc/fstab | awk '{print $1}'`
ROOTPART=`echo $ROOTPART | sed -e 's/\//\\\\\\//g'`
ROOTENTRY=`echo $ROOTENTRY | sed -e 's/\//\\\\\\//g'`
sed -i -r "s/$ROOTPART/$ROOTENTRY/g" $ROOTMOUNT/boot/grub/menu.lst
#in the case of older distros that use hd for IDE and sd for SATA, rename sd back to hd and try again - no change if it doesn't exist
ROOTPART=`echo $ROOTPART | sed -e 's/s/h/g'`
sed -i -r "s/$ROOTPART/$ROOTENTRY/g" $ROOTMOUNT/boot/grub/menu.lst



os-prober > /tmp/os-prober.$$

cat /tmp/os-prober.$$ | while read LINE ; do

DEV=`echo $LINE | awk -F ":" '{print $1}'`
TITLE=`echo $LINE | awk -F ":" '{print $2}'`
OSTYPE=`echo $LINE | awk -F ":" '{print $3}'`
GRUBTYPE=`echo $LINE | awk -F ":" '{print $4}'`
GRUBDEV=`echo $DEV | awk -F "/" '{print $3}'`
GRUBPARTNUM=`echo $GRUBDEV | sed -e 's/[a-z]//g'`
GRUBPARTNUM=`expr $GRUBPARTNUM - 1`
GRUBA=`echo $GRUBDEV | sed -e 's/[0-9]//'`
GRUBB=`echo $GRUBA | sed -e 's/[sh]d//'`
cnt=0
for i in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
if [ "$GRUBB" = "$i" ]; then
GRUBDEVPART="hd$cnt,$GRUBPARTNUM"
fi
cnt=`expr $cnt + 1`
done

#removed below as it was inconsistent for Windows partitions
#GRUBDEVPART=`grub-probe -m $ROOTMOUNT/boot/grub/device.map -d $DEV -t drive`


if [ "$DEV" != "$ROOTPART" ]; then

echo "Setting $DEV up in menu.lst"

if [ "$GRUBTYPE" = "chain" ]; then
cat >> $GRUBFILE <<FOO

# $TYPE
title		$OSTYPE - $TITLE
root		($GRUBDEVPART)
makeactive
chainloader	+1

FOO
fi

if [ "$GRUBTYPE" = "linux" ]; then

if [ ! -d /mnt/remastersys ]; then
mkdir -p /mnt/remastersys
fi

if [ `mount | grep $DEV | awk '{print$1}'` ]; then
umount -f $DEV
sleep 1
fi

mount $DEV /mnt/remastersys -o ro


for i in `ls -1 /mnt/remastersys/boot/vmlinuz*` ;do
KERNELNAME=`echo $i | sed -e 's/\/mnt\/remastersys//'`
KERNELVER=`echo $KERNELNAME | sed -e 's/\/boot\/vmlinuz//'` 
INITRDNAME=`ls -1 /mnt/remastersys/boot/initrd*$KERNELVER | sed -e 's/\/mnt\/remastersys//'`
ROOTENTRY=`grep " / " /mnt/remastersys/etc/fstab | awk '{print $1}'`

SPLASHENTRY=""
TESTVGA=`grep "$ROOTENTRY" /mnt/remastersys/boot/grub/menu.lst | grep -m 1 "^kernel" | grep "vga="`
TESTSPLASH=`grep "$ROOTENTRY" /mnt/remastersys/boot/grub/menu.lst | grep -m 1 "^kernel" | grep "splash"`
TESTQUIET=`grep "$ROOTENTRY" /mnt/remastersys/boot/grub/menu.lst | grep -m 1 "^kernel" | grep "quiet"`

if [ "$TESTQUIET" != "" ]; then
for j in `echo $TESTQUIET`; do
if [ `echo "$j" | grep "quiet"` ]; then
SPLASHENTRY="$SPLASHENTRY $j"
fi
done
fi
if [ "$TESTSPLASH" != "" ]; then
for j in `echo $TESTSPLASH`; do
if [ `echo "$j" | grep "splash"` ]; then
SPLASHENTRY="$SPLASHENTRY $j"
fi
done
fi
if [ "$TESTVGA" != "" ]; then
for j in `echo $TESTVGA`; do
if [ `echo "$j" | grep "vga"` ]; then
SPLASHENTRY="$j"
fi
done
fi


if [ -f /mnt/remastersys$INITRDNAME ]; then
cat >> $GRUBFILE <<FOO

title		$TITLE, kernel$KERNELVER
root		($GRUBDEVPART)
kernel		$KERNELNAME root=$ROOTENTRY ro $SPLASHENTRY
initrd		$INITRDNAME

title		$TITLE, kernel$KERNELVER (single-user mode)
root		($GRUBDEVPART)
kernel		$KERNELNAME root=$ROOTENTRY ro single
initrd		$INITRDNAME

FOO
else
cat >> $GRUBFILE <<FOO

title		$TITLE, kernel$KERNELVER
root		($GRUBDEVPART)
kernel		$KERNELNAME root=$ROOTENTRY ro $SPLASHENTRY

title		$TITLE, kernel$KERNELVER (single-user mode)
root		($GRUBDEVPART)
kernel		$KERNELNAME root=$ROOTENTRY ro single

FOO
fi

done

umount $DEV

fi

fi

done
if [ -d /mnt/remastersys ]; then
umount -f /mnt/remastersys
fi
rm -f /tmp/os-prober.$$ 


